package org.wenhuayuan.project.common.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.text.InputType
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import org.wenhuayuan.project.common.R

class InputItemLayout:LinearLayout {
    private lateinit var titleView: TextView
    private lateinit var editText: EditText
    private var bottomLine: Line
    private var topLine: Line
    private var topPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var bottomPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    constructor(context: Context):this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?):this(context, attributeSet,0)
    constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int):super(context, attributeSet, defStyle) {
        val array = context.obtainStyledAttributes(attributeSet, R.styleable.InputItemLayout)


        // 解析 <declare-styleable name="titleTextAppearance"> 中的属性
        val titleStyleId = array.getResourceId(R.styleable.InputItemLayout_titleTextAppearance, 0)
        val title = array.getString(R.styleable.InputItemLayout_title)
        parseTitleStyle(titleStyleId, title)


        // 解析 <declare-styleable name="inputTextAppearance"> 中的属性
        val inputStyleId = array.getResourceId(R.styleable.InputItemLayout_inputTextAppearance, 0)
        val hint = array.getString(R.styleable.InputItemLayout_hint)
        val inputType = array.getInteger(R.styleable.InputItemLayout_inputType, 0)
        parseInputStyle(inputStyleId, hint, inputType)


        // 解析 <declare-styleable name="lineAppearance"> 上下分割线中的属性
        val topLineStyleId = array.getResourceId(R.styleable.InputItemLayout_topLineAppearance, 0)
        val bottomLineStyleId = array.getResourceId(R.styleable.InputItemLayout_bottomLineAppearance, 0)
        topLine = parseLineStyleId(topLineStyleId)
        bottomLine = parseLineStyleId(bottomLineStyleId)
        if (topLine.enable) {
            topPaint.color = topLine.color
            topPaint.style = Paint.Style.FILL_AND_STROKE
            topPaint.strokeWidth = topLine.height.toFloat()
        }

        if (bottomLine.enable) {
            bottomPaint.color = bottomLine.color
            bottomPaint.style = Paint.Style.FILL_AND_STROKE
            bottomPaint.strokeWidth = bottomLine.height.toFloat()
        }
        array.recycle()

    }

    private fun parseLineStyleId(styleResId: Int) : Line {
        var line = Line()
        val typeArray =
            context.obtainStyledAttributes(styleResId, R.styleable.lineAppearance)
        line.color = typeArray.getColor(R.styleable.lineAppearance_color, resources.getColor(R.color.color_d1d2))
        line.height = typeArray.getDimensionPixelOffset(R.styleable.lineAppearance_height, 0)
        line.leftMargin = typeArray.getDimensionPixelOffset(R.styleable.lineAppearance_leftMargin, 0)
        line.rightMargin = typeArray.getDimensionPixelOffset(R.styleable.lineAppearance_rightMargin, 0)
        line.enable = typeArray.getBoolean(R.styleable.lineAppearance_enable, false)
        typeArray.recycle()
        return line
    }
    inner class Line {
        var color = 0
        var height = 0
        var leftMargin = 0
        var rightMargin = 0
        var enable = false
    }

    private fun parseInputStyle(inputStyleId: Int, hint: String?, inputType: Int) {
        val typeArray =
            context.obtainStyledAttributes(inputStyleId, R.styleable.inputTextAppearance)
        val hintColor = typeArray.getColor(R.styleable.inputTextAppearance_hintColor, resources.getColor(R.color.color_d1d2))
        val inputColor = typeArray.getColor(R.styleable.inputTextAppearance_inputColor, resources.getColor(R.color.color_565))
        val textSize = typeArray.getDimensionPixelSize(R.styleable.inputTextAppearance_textSize, applyUnit(TypedValue.COMPLEX_UNIT_SP, 14f))

        editText = EditText(context)
        val params = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        params.weight = 1f
        editText.layoutParams = params
        editText.setHintTextColor(hintColor)
        editText.setHint(hint)
        editText.setTextColor(inputColor)
        editText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
        editText.setBackgroundColor(Color.TRANSPARENT)
        editText.gravity = Gravity.LEFT or Gravity.CENTER


        if (inputType == 0) {
            editText.inputType = InputType.TYPE_CLASS_TEXT
        } else if (inputType == 1) {
            editText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD or (InputType.TYPE_CLASS_TEXT)
        } else if (inputType == 2) {
            editText.inputType = InputType.TYPE_CLASS_NUMBER
        }
        addView(editText)
        typeArray.recycle()

    }

    private fun parseTitleStyle(titleStyleId: Int, title: String?) {
        val array = context.obtainStyledAttributes(titleStyleId, R.styleable.titleTextAppearance)
        val titleColor = array.getColor(
            R.styleable.titleTextAppearance_titleColor,
            resources.getColor(R.color.color_565)
        )

        //px
        val titleSize = array.getDimensionPixelSize(
            R.styleable.titleTextAppearance_titleSize,
            applyUnit(TypedValue.COMPLEX_UNIT_SP, 15f)
        )

        val minWidth = array.getDimensionPixelOffset(R.styleable.titleTextAppearance_minWidth, 0)

        titleView = TextView(context)
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize.toFloat())  //sp---当做sp在转换一次
        titleView.setTextColor(titleColor)
        titleView.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
        titleView.minWidth = minWidth
        titleView.gravity = Gravity.LEFT or (Gravity.CENTER)
        titleView.text = title
        addView(titleView)
        array.recycle()
    }

    /**
     * @param unit={#TypedValue.COMPLEX_UNIT_SP}
     */
    private fun applyUnit(unit: Int, value: Float) : Int {
        return TypedValue.applyDimension(unit, value, resources.displayMetrics).toInt()
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (topLine.enable) {
            canvas?.drawLine(topLine.leftMargin.toFloat(), 0f, (measuredWidth - topLine.rightMargin).toFloat(), 0f, topPaint)
        }
        if (bottomLine.enable) {
            canvas?.drawLine(bottomLine.leftMargin.toFloat(), height - bottomLine.height.toFloat(), (measuredWidth - bottomLine.rightMargin).toFloat(), height - bottomLine.height.toFloat(), bottomPaint)
        }
    }

    fun getTitleView(): TextView {
        return titleView
    }

    fun getEditText(): EditText {
        return editText
    }

}
